class Solution {
public:
    int calc(vector<vector<int>> &grid, int x, int y) {
        set<int> lst, rst;
        for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
            lst.insert(grid[i][j]);
        }
        for (int i = x + 1, j = y + 1; i < grid.size() && j < grid[0].size(); i++, j++) {
            rst.insert(grid[i][j]);
        }
        return abs(static_cast<int>(lst.size()) - static_cast<int>(rst.size()));
    }
    vector<vector<int>> differenceOfDistinctValues(vector<vector<int>>& grid) {
        vector<vector<int>> arr(grid.size(), vector<int>(grid[0].size(), 0));
        if (arr.size() == 1) return arr;
        int m = grid.size(), n = grid[0].size();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = calc(grid, i, j);
            }
        }
        return arr;
    }
};
